package com.qdy.api;

import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;
import com.qdy.entity.exercise.Exercise;
import com.qdy.entity.exercise.ExerciseComment;
import com.qdy.entity.exercise.ExerciseMember;
import com.qdy.entity.user.User;

import com.qdy.repository.ExerciseCommentRepository;
import com.qdy.repository.ExerciseMemberRepository;
import com.qdy.repository.ExerciseRepository;
import com.qdy.service.ExerciseService;
import com.qdy.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 活动API接口
 * @version  v1
 * Created by 方礼爱 on 2017/3/26.
 */
@RequestMapping("/api/v1/exercise")
@RestController
public class ExerciseAPIController  {
    @Autowired
    private ExerciseService exerciseService;
    /**
     * 加载活动祥情
     * @param id
     * @return
     */
    @RequestMapping(value="/getInfo/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadExerciseInfo(@PathVariable("id") Long id)throws ServiceException {

        return  RestResultGenerator.genSuccessResult(exerciseService.getExerciseInfo(id));
    }

    /**
     * 活动报名
     * @param member
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value="/enroll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object enroll(ExerciseMember member)throws ServiceException {
            try{
                User user=new User();
                user.setId(HttpUtil.getUserId());
                member.setUser(user);
                member.setState(0);
                exerciseService.saveMember(member);
                return  RestResultGenerator.genSuccessResult();
             }catch (Exception e){
                throw  new ServiceException("报名失败",e.getMessage());
            }
    }


    /**
     * 创建团队
     * @param exercise
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value="/create",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object create( Exercise exercise)throws ServiceException {
        try{
            User user=new User();
            user.setId(HttpUtil.getUserId());
            exercise.setUser(user);
            exercise.setCreateTime(new Date());
            exerciseService.saveExercise(exercise);
            return RestResultGenerator.genSuccessResult("创建成功");
        }catch (Exception e){
            throw  new ServiceException("创建活动失败!",e.getMessage());
        }


    }

    /**
     * 发表评论
     * @param comment
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value="/comment",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object comment(ExerciseComment comment)throws ServiceException {
        try{
            comment.setCommentTime(new Date());
            comment.setUser(HttpUtil.getUser());
            exerciseService.saveComment(comment);
            return RestResultGenerator.genSuccessResult(comment);
        }catch (Exception e){
            throw new ServiceException("发表失败",e.getMessage());
        }


    }

    @RequestMapping(value="/loadAll",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadExercise()throws ServiceException {
        return RestResultGenerator.genSuccessResult(exerciseService.findExerciseByAll()) ;
    }
}
